package com.aaron.kjviewutils.stock;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aaron.kjviewutils.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Aaron on 2017/3/2.
 */

public class StockInnerAdapter extends RecyclerView.Adapter<StockInnerAdapter.StockInnerViewHolder> {

    private List<StockBean.StockInner> datas;
    private Context context;
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private View mHeaderView;
    private boolean hasHead = false;

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        hasHead = true;
        notifyItemInserted(0);
    }

    public boolean isHasHead() {
        return hasHead;
    }

    @Override
    public int getItemViewType(int position) {
        if (!hasHead)
        {
            return TYPE_NORMAL;
        }
        else
        {
            if(0 == position)
            {
                return TYPE_HEADER;
            }
            return TYPE_NORMAL;
        }
    }

    public StockInnerAdapter(Context context, List<StockBean.StockInner> datas)
    {
        this.context = context;
        this.datas = datas;
    }

    private OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public StockInnerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(hasHead) {
            if(viewType == TYPE_HEADER)
            {
                return new StockInnerViewHolder(mHeaderView);
            }
            else
            {
                View view = LayoutInflater.from(context).inflate(R.layout.item_stockinner, parent, false);
                return new StockInnerViewHolder(view);
            }
        }
        else
        {
            View view = LayoutInflater.from(context).inflate(R.layout.item_stockinner, parent, false);
            return new StockInnerViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(final StockInnerViewHolder holder, int position) {
        if(hasHead)
        {
            if(getItemViewType(position) == TYPE_NORMAL)
            {
                if(null!=datas&&!datas.isEmpty()&&null!=datas.get(position-1))
                {
                    final StockBean.StockInner stock = datas.get(position-1);
                    holder.stockName.setText(stock.name);
                    holder.stockCode.setText(stock.code);
                    if(null!=mOnItemClickListener)
                    {
                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                            }
                        });
                    }
                }
            }
        }
        else
        {
            if(null!=datas&&!datas.isEmpty()&&null!=datas.get(position))
            {
                final StockBean.StockInner stock = datas.get(position);
                holder.stockName.setText(stock.name);
                holder.stockCode.setText(stock.code);
                if(null!=mOnItemClickListener)
                {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                        }
                    });
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        if(!hasHead)
        {
            return (null==datas)?0:datas.size();
        }else
        {
            return (null==datas)?1:datas.size()+1;
        }
    }

    public class StockInnerViewHolder extends RecyclerView.ViewHolder
    {
        @Bind(R.id.stockName)
        TextView stockName;
        @Bind(R.id.stockCode)
        TextView stockCode;

        public StockInnerViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView){
                return;
            }
            else
            {
                ButterKnife.bind(this, itemView);
            }
        }
    }

    /**
     * item点击事件
     */
    public interface OnItemClickListener {
        /**
         * item点击事件
         *
         * @param view
         * @param position
         */
        void onItemClick(View view, int position);
    }
}
